/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.ModeloMaterial;
import com.ipartek.formacion.gestformwww.modelo.beans.Material;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Fran
 */
public class DetallarPedidoAccion implements Accion {

    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion;
        sesion = request.getSession();
        AccionRouter router;
        AccionServlet as = (AccionServlet) servlet;
        int materialId = 0;
        ArrayList<String> errors = new ArrayList<String>();

        ModeloMaterial modelo = (ModeloMaterial) servlet.getServletContext().getAttribute("ModeloMaterial");
        Material materialMostrar = null;



        if (sesion.getAttribute("usuario") == null) {
            router = new AccionRouter("inicio", false);
        } else {
            router = new AccionRouter("principal");

//            if (request.getParameterValues("listaIdMaterial") != null) {
//                materialId = Integer.parseInt(request.getParameterValues("listaIdMaterial")[0]);
            if (request.getParameter("idMaterial") != null && (!request.getParameter("idMaterial").isEmpty())) {
                materialId = Integer.parseInt(request.getParameter("idMaterial"));
                try {
                    materialMostrar = modelo.getMaterial(materialId);

                } catch (GestorFormacionExcepcion ex) {
                    Logger.getLogger(DetallarPedidoAccion.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else {

                //request.setAttribute("contenido", "listar-material-importar");
                errors.add("mensajes-seleccionar-material-error");
                request.setAttribute("mensajes", errors);
                request.setAttribute("idCurso", request.getParameter("idCurso"));
                try {
                    router = as.getFactory().getAction(as.getNombreClaseRecurso("importar-material-accion"), servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            if (materialMostrar != null) {
                request.setAttribute("idCurso", request.getParameter("idCurso"));
                request.setAttribute("contenido", "detalles-pedido");
                request.setAttribute("materialMostrar", materialMostrar);

            } else {
                errors.add("mensajes-seleccionar-material-error");
                request.setAttribute("mensajes", errors);
                try {
                    router = as.getFactory().getAction(as.getNombreClaseRecurso("importar-material-accion"), servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(BorrarFaltaAccion.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }

        return router;
    }
}
